/*--------------------------------------------------------------------------------------------------
Yatak Operation System Project Copyright 2012
Author : Ilya Churaev
Started : 30 Apr 2012		Updated : 30 Apr 2012
File Name : time.c			Purpose : get time & date
--------------------------------------------------------------------------------------------------*/
#include <cmos.h>
#include <time.h>
#include <screen.h>

void get_time(systime *time)
{
	time->second = cmos_read(SECOND);
	time->minute = cmos_read(MINUTE);
	time->hour   = cmos_read(HOUR);
}

unsigned int get_time_sec ()
{
	return (cmos_read(SECOND) + cmos_read(MINUTE)*60 + cmos_read(HOUR)*60*60);
}

unsigned int get_sec_from_time (const systime time)
{
	return (time.second + time.minute*60 + time.hour*60*60);
}

void get_time_count (systime *time, unsigned int stime, unsigned int etime)
{
	unsigned int dtime;
	if ( etime >= stime)
		dtime = etime - stime;
	else
		dtime = stime - etime;
	
	time->hour = (dtime - (dtime%3600))/3600;
	dtime = dtime - time->hour*3600;
	time->minute = (dtime - (dtime%60))/60;
	dtime = dtime - time->minute*60;
	time->second = dtime;
}

void get_difference_times (systime *time, const systime stime, const systime etime)
{
	get_time_count (time,get_sec_from_time(stime),get_sec_from_time(etime));
}

void get_date(sysdate *date)
{
	date->year  = cmos_read(YEAR);
	date->month = cmos_read(MONTH);
	date->date  = cmos_read(DATE);
	date->day   = cmos_read(DAY);
}

void timeprint(const systime time)
{
	if (time.hour < 10)
		kprintf ("0");
	kprintf ("%d:",time.hour);
	if (time.minute < 10)
		kprintf ("0");
	kprintf ("%d:",time.minute);
	if (time.second < 10)
		kprintf ("0");
	kprintf ("%d",time.second);
}

void get_str_time (char *str,const systime time)
{
	char hour [3], min [3], sec [3];
	if (time.hour < 10)
		sprintf (hour,"0%d",time.hour);
	else
		sprintf (hour,"%d",time.hour);
	if (time.minute < 10)
		sprintf (min,"0%d",time.minute);
	else
		sprintf (min,"%d:",time.minute);
	if (time.second < 10)
		sprintf (sec,"0%d",time.second);
	else
		sprintf (sec,"%d",time.second);
	sprintf(str,"%s:%s:%s\0",hour, min,sec);
}

void dateprint(const sysdate date)
{
	kprintf ("%d ", date.date);
	switch (date.month)
	{
	case 1:
		kprintf ("January");
		break;
	case 2:
		kprintf ("February");
		break;
	case 3:
		kprintf ("March");
		break;
	case 4:
		kprintf ("April");
		break;
	case 5:
		kprintf ("May");
		break;
	case 6:
		kprintf ("June");
		break;
	case 7:
		kprintf ("July");
		break;
	case 8:
		kprintf ("August");
		break;
	case 9:
		kprintf ("September");
		break;
	case 10:
		kprintf ("October");
		break;
	case 11:
		kprintf ("November");
		break;
	case 12:
		kprintf ("December");
		break;
	}
	kprintf (" %d ",date.year);
	switch (date.day)
	{
	case 1:
		kprintf ("Sunday");
		break;
	case 2:
		kprintf ("Monday");
		break;
	case 3:
		kprintf ("Tuesday");
		break;
	case 4:
		kprintf ("Wednesday");
		break;
	case 5:
		kprintf ("Thursday");
		break;
	case 6:
		kprintf ("Friday");
		break;
	case 7:
		kprintf ("Saturday");
		break;
	}
}

void get_str_date (char * str, const sysdate date)
{
	char day [3], month [10],year [10], day_w [10];
	sprintf (day,"%d", date.date);
	switch (date.month)
	{
	case 1:
		sprintf (month,"January");
		break;
	case 2:
		sprintf (month,"February");
		break;
	case 3:
		sprintf (month,"March");
		break;
	case 4:
		sprintf (month,"April");
		break;
	case 5:
		sprintf (month,"May");
		break;
	case 6:
		sprintf (month,"June");
		break;
	case 7:
		sprintf (month,"July");
		break;
	case 8:
		sprintf (month,"August");
		break;
	case 9:
		sprintf (month,"September");
		break;
	case 10:
		sprintf (month,"October");
		break;
	case 11:
		sprintf (month,"November");
		break;
	case 12:
		sprintf (month,"December");
		break;
	}
	sprintf (year,"%d",date.year);
	switch (date.day)
	{
	case 1:
		sprintf (day_w,"Sunday");
		break;
	case 2:
		sprintf (day_w,"Monday");
		break;
	case 3:
		sprintf (day_w,"Tuesday");
		break;
	case 4:
		sprintf (day_w,"Wednesday");
		break;
	case 5:
		sprintf (day_w,"Thursday");
		break;
	case 6:
		sprintf (day_w,"Friday");
		break;
	case 7:
		sprintf (day_w,"Saturday");
		break;
	}
	sprintf (str,"%s %s %s %s\0",day, month, year, day_w);
}
